package com.thx.file;

import java.io.File;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.thx.common.Constant;
import com.thx.common.util.AppContext;

public class Configurations
{
  static final String CONFIG_FILE = "stream-config.properties";
  private static Properties properties = null;
  private static final boolean STREAM_DELETE_FINISH = false;//文件上传完之后，是否删除
  private static final boolean STREAM_IS_CROSS = false;//是否允许其他域上传文件至本服务器
  private static final String STREAM_CROSS_ORIGIN = "*";
  public static String getConfig(String key) {
    return getConfig(key, null);
  }

  public static String getConfig(String key, String defaultValue) {
    return properties.getProperty(key, defaultValue);
  }

  public static int getConfig(String key, int defaultValue) {
    String val = getConfig(key);
    int setting = 0;
    try {
      setting = Integer.parseInt(val);
    } catch (NumberFormatException e) {
      setting = defaultValue;
    }
    return setting;
  }
//获取文件上传路径
  public static String getFileRepository() {
	  String path = AppContext.getProperties(Constant.UPLOAD_PATH_KEY);
		if(StringUtils.isBlank(path)){
			path =  ServletActionContext.getServletContext().getRealPath(java.io.File.separator);
			path += "upload";
		}
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		return path;
  }

  public static String getCrossServer() {
    return "";
  }

  public static String getCrossOrigins() {
    return  STREAM_CROSS_ORIGIN;
  }

  public static boolean getBoolean(String key) {
    return Boolean.parseBoolean(getConfig(key));
  }

  public static boolean isDeleteFinished() {
   // return getBoolean("STREAM_DELETE_FINISH");
	  return STREAM_DELETE_FINISH;
  }

  public static boolean isCrossed() {
    return STREAM_IS_CROSS;
  }

}